import axios from "axios";

export const request = axios.create({
  timeout: 10000,
  timeoutErrorMessage: "请求超时....",
});

request.interceptors.request.use(
  (config) => {
    // 携带token
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    const { code } = response.data;
    switch (code) {
      case 200:
        console.log("请求数据成功");
        break;
      case 400:
      case 401:
      case 402:
      case 403:
        console.log("客服端错误");
        break;
      case 500:
      case 501:
      case 502:
      case 503:
        console.log("服务端错误");
    }

    return response;
  },
  (error) => {
    console.log(error);

    const { status } = error;

    if (status >= 400 && status < 500) {
      console.log("客户端错误");
    } else if (status >= 500) {
      console.log("服务端错误");
    }
    return Promise.reject(error);
  }
);
